// dxCommon.h: interface for the dxCommon class.
//
//////////////////////////////////////////////////////////////////////

#ifndef _dxCommon_h_
#define _dxCommon_h_

#include <windows.h>
#include <stdio.h>
#include <d3d9.h>
#include <d3dx9.h>

//Use ImageFile In DevMode
#ifdef USE_ENCODING_TEXTURE
#	define PATH_TEXTURE	".\\DATA\\Encoded_Texture\\"
#	define PATH_INTERFACE	".\\DATA\\Encoded_Interface\\"

// Use Encode Image File In Release Mode
#else
#	define PATH_TEXTURE	".\\DATA\\Texture\\"
#	define PATH_INTERFACE	".\\DATA\\Interface\\"
#endif

// Others Use Sameone
#define PATH_MESH		".\\DATA\\Mesh\\"
#define PATH_TRACK		".\\DATA\\Track\\"
#define PATH_SOUND		".\\DATA\\Sound\\"
#define PATH_LOBBY		".\\DATA\\Lobby\\"


#define	_EPSILON	1.0e-6

#define	FILE_NAME	256


#define _SafeDelete(t)		{ if((t)) { delete	  (t);     (t)=NULL; } }
#define _SafeAllDelete(t)	{ if((t)) { delete [] (t);     (t)=NULL; } }
#define _SafeRelease(t)		{ if((t)) { (t)->Release();	   (t)=NULL; } }

enum	MOUSE_PUSH_STATE	{	MP_NORMAL, MPL_CLICK, MPL_DOWN, MPL_UP, 
								MPR_CLICK, MPR_DOWN, MPR_UP};
enum	MOUSE_ORDER			{	MO_NORMAL, MO_GATE, MO_ENEMY, MO_VILL ,MO_SELECT,MO_ITEM, MO_GEMBOX};

#define MY_D3DFVF	(D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
struct  MyVertex	
{ 
	D3DXVECTOR3 pos; 
	D3DXVECTOR3 n; 
	FLOAT tu, tv; 

	void Set(const D3DXVECTOR3 p, const D3DXVECTOR3 c, const float u, const float v)	{ pos = p; n = c; tu = u; tv = v; }
};

#define MY_D3DTFVF	(D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
struct  MyTVertex	
{ 
	D3DXVECTOR3 pos; 
	DWORD color; 
	FLOAT tu, tv; 

	void Set(const D3DXVECTOR3 p, const DWORD c, const float u, const float v)	{ pos = p; color = c; tu = u; tv = v; }
};

#define MY_D3DRHWTFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)
struct MyRhwTVertex
{
	D3DXVECTOR3 pos;
	float		rhw;
	DWORD		color;
	float		tu, tv;

	void Set(const D3DXVECTOR3 p, const float w, const DWORD c, const float u, const float v)	{ pos = p; rhw = w; color = c; tu = u; tv = v; }
};

//2005.6.13. Chae, Youn-Sil
#define MY_D3DFVF_SIMPLE	(D3DFVF_XYZ | D3DFVF_DIFFUSE)
struct  MyVertexSimple
{ 
	D3DXVECTOR3	pos;
	DWORD		color;

	void Set(const D3DXVECTOR3 p, const DWORD c)	{ pos = p; color = c;}
};
#define MY_D3DTCOORD	(D3DFVF_TEX0)
struct  MyTextureCoord
{ 
	FLOAT tu, tv; 

	void Set(const float u, const float v)	{ tu = u; tv = v; }
};
//-----------------------------------------------------------------------------SpringSun

inline float Random() 
{
	return rand() * 0.0000305f - 0.5f;
}

inline D3DCOLOR GetGrayColor(float s)
{
	int c = int(s*255);
	return D3DCOLOR(c | c<<8 | c<<16 | c<<24);
}

inline float GetAngle(D3DXVECTOR3 dir)
{
	if(sqrtf(dir.x*dir.x + dir.z*dir.z) == 0 )	return acosf(0.f);

	float th = dir.x / sqrtf(dir.x*dir.x + dir.z*dir.z);
	return (dir.z < 0) ? -acosf(th) : acosf(th);
}

inline float GetAngle(float x, float y)
{
	if(sqrtf(x*x + y*y) == 0 )	return acosf(0.f);

	float th = x / sqrtf(x*x + y*y);		
	return (y < 0) ? -acosf(th) : acosf(th);
}

inline float GetAngleNormalX(float x, float y)
{
	return (y < 0) ? -acosf(x) : acosf(x);
}

extern HWND			g_hWnd;
extern HINSTANCE	g_hInstance;

extern DWORD	g_dwWidth;
extern DWORD	g_dwHeight;

extern bool		g_bActive;
extern bool		g_bWindow;
extern bool		g_bShowFPS;
extern char		g_ShortMsg[9][64];
extern int		g_Key[6];

inline void MsgBox (LPSTR MSG) 
{
	MessageBox(g_hWnd, MSG, "RACING", MB_OK);
}

inline void MsgBox (const char *MSG) 
{
	MessageBox(g_hWnd, MSG, "RACING", MB_OK);
}

#define CaseMsgBox(a) case(a): MsgBox(#a); break

inline bool	CheckPtInRect (RECT rect, POINT point)
{
	return ( point.x > rect.left && point.x < rect.right &&  point.y > rect.top && point.y < rect.bottom );
}

inline RECT	MakeRect (POINT Point, int Height, int Width)
{
	RECT rect;

	rect.left	= Point.x;
	rect.top	= Point.y;
	rect.right	= rect.left + Width;
	rect.bottom = rect.top + Height;

	return rect;
}

#endif
